home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / usr_10.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  28.3 KB  |  823 lines

  1. *usr_10.txt*    For Vim version 6.0.  Last change: 2001 Sep 18
  2.  
  3.              VIM USER MANUAL - by Bram Moolenaar
  4.  
  5.                  Making big changes
  6.  
  7.  
  8. In chapter 4 several ways to make small changes were explained.  This chapter
  9. goes into making changes that are repeated or can affect a large amount of
  10. text.  The Visual mode allows doing various things with blocks of text.  Use
  11. an external program to do really complicated things.
  12.  
  13. |10.1|    Record and playback commands
  14. |10.2|    Substitution
  15. |10.3|    Command ranges
  16. |10.4|    The global command
  17. |10.5|    Visual block mode
  18. |10.6|    Reading and writing part of a file
  19. |10.7|    Formatting text
  20. |10.8|    Changing case
  21. |10.9|    Using an external program
  22.  
  23.      Next chapter: |usr_11.txt|  Recovering from a crash
  24.  Previous chapter: |usr_09.txt|  Using the GUI
  25. Table of contents: |usr_toc.txt|
  26.  
  27. ==============================================================================
  28. *10.1*    Record and playback commands
  29.  
  30. The "." command repeats the preceding change.  But what if you want to do
  31. something more complex than a single change?  That's where command recording
  32. comes in.  There are three steps:
  33.  
  34. 1. The "q{register}" command starts recording keystrokes into the register
  35.    named {register}.  The register name must be between a and z.
  36. 2. Type your commands.
  37. 3. To finish recording, press q (without any extra character).
  38.  
  39. You can now execute the macro by typing the command "@{register}".
  40.  
  41. Take a look at how to use these commands in practice.  You have a list of
  42. filenames that look like this:
  43.  
  44.     stdio.h ~
  45.     fcntl.h ~
  46.     unistd.h ~
  47.     stdlib.h ~
  48.  
  49. And what you want is the following:
  50.  
  51.     #include "stdio.h" ~
  52.     #include "fcntl.h" ~
  53.     #include "unistd.h" ~
  54.     #include "stdlib.h" ~
  55.  
  56. You start by moving to the first character of the first line.  Next you
  57. execute the following commands:
  58.  
  59.     qa            Start recording a macro in register a.
  60.     ^            Move to the beginning of the line.
  61.     i#include "<Esc>    Insert the string #include " at the beginning
  62.                 of the line.
  63.     $            Move to the end of the line.
  64.     a"<Esc>            Append the character double quotation mark (")
  65.                 to the end of the line.
  66.     j            Go to the next line.
  67.     q            Stop recording the macro.
  68.  
  69. Now that you have done the work once, you can repeat the change by typing the
  70. command "@a" three times.
  71. The "@a" command can be preceded by a count, which will cause the macro to be
  72. executed that number of times.  In this case you would type: >
  73.  
  74.     3@a
  75.  
  76.  
  77. MOVE AND EXECUTE
  78.  
  79. You might have the lines you want to change in various places.  Just move the
  80. cursor to each location and use the "@a" command.  If you have done that once,
  81. you can do it again with "@@".  That's a bit easier to type.  If you now
  82. execute register b with "@b", the next "@@" will use register b.
  83.    If you compare the playback method with using ".", there are several
  84. differences.  First of all, "." can only repeat one change.  As seen in the
  85. example above, "@a" can do several changes, and move around as well.
  86. Secondly, "." can only remember the last change.  Executing a register allows
  87. you to make any changes and then still use "@a" to replay the recorded
  88. commands.  Finally, you can use 26 different registers.  Thus you can remember
  89. 26 different command sequences to execute.
  90.  
  91.  
  92. USING REGISTERS
  93.  
  94. The registers used for recording are the same ones you used for yank and
  95. delete commands.  This allows you to mix recording with other commands to
  96. manipulate the registers.
  97.    Suppose you have recorded a few commands in register n.  When you execute
  98. this with "@n" you notice you did something wrong.  You could try recording
  99. again, but perhaps you will make another mistake.  Instead, use this trick:
  100.  
  101.     G            Go to the end of the file.
  102.     o<Esc>            Create an empty line.
  103.     "np            Put the text from the n register.  You now see
  104.                 the commands you typed as text in the file.
  105.     {edits}            Change the commands that were wrong.  This is
  106.                 just like editing text.
  107.     0            Go to the start of the line.
  108.     "ny$            Yank the corrected commands into the n
  109.                 register.
  110.     dd            Delete the scratch line.
  111.  
  112. Now you can execute the corrected commands with "@n".  (If your recorded
  113. commands include line breaks, adjust the last two items in the example to
  114. include all the lines).
  115.  
  116.  
  117. APPENDING TO A REGISTER
  118.  
  119. So far we have used a lowercase letter for the register name.  To append to a
  120. register, use an uppercase letter.
  121.    Suppose you have recorded a command to change a word to register c.  It
  122. works properly, but you would like to add a search for the next word to
  123. change.  This can be done with: >
  124.  
  125.     qC/word<Enter>q
  126.  
  127. You start with "qC", which records to the c register and appends.  Thus
  128. writing to an uppercase register name means to append to the register with
  129. the same letter, but lowercase.
  130.  
  131. This works both with recording and with yank and delete commands.  For
  132. example, you want to collect a sequence of lines into the a register.  Yank
  133. the first line with: >
  134.  
  135.     "aY
  136.  
  137. Now move to the second line, and type: >
  138.  
  139.     "AY
  140.  
  141. Repeat this command for all lines.  The a register now contains all those
  142. lines, in the order you yanked them.
  143.  
  144. ==============================================================================
  145. *10.2*    Substitution
  146.  
  147. The ":substitute" command enables you to perform string replacements on a
  148. whole range of lines.  The general form of this command is as follows: >
  149.  
  150.     :[range]substitute/from/to/[flags]
  151.  
  152. This command changes the "from" string to the "to" string in the lines
  153. specified with [range].  For example, you can change "Professor" to "Teacher"
  154. in all lines with the following command: >
  155.  
  156.     :%substitute/Professor/Teacher/
  157. <
  158.     Note:
  159.     The :substitute command is almost never spelled out completely.  Most
  160.     of the time, people use the abbreviated version ":s".  From here on
  161.     the abbreviation will be used.
  162.  
  163. The "%" before the command specifies the command works on all lines.  Without
  164. a range, ":s" only works on the current line.  More about ranges in the next
  165. section.
  166.  
  167. By default, the ":substitute" command changes only the first occurrence on
  168. each line.  For example, the preceding command changes the line:
  169.  
  170.     Professor Smith criticized Professor Johnson today. ~
  171.  
  172. to:
  173.  
  174.     Teacher Smith criticized Professor Johnson today. ~
  175.  
  176. To change every occurrence on the line, you need to add the g (global) flag.
  177. The command: >
  178.  
  179.     :%s/Professor/Teacher/g
  180.  
  181. results in (starting with the original line):
  182.  
  183.     Teacher Smith criticized Teacher Johnson today. ~
  184.  
  185. Other flags include p (print), which causes the ":substitute" command to print
  186. out each line it changes.  The c (confirm) flag tells ":substitute" to ask you
  187. for confirmation before it performs each substitution.  Enter the following: >
  188.  
  189.     :%s/Professor/Teacher/c
  190.  
  191. Vim finds the first occurrence of "Professor" and displays the text it is
  192. about to change.  You get the following prompt: >
  193.  
  194.     replace with Teacher (y/n/a/q/l/^E/^Y)?
  195.  
  196. At this point, you must enter one of the following answers:
  197.  
  198.     y        Yes; make this change.
  199.     n        No; skip this match.
  200.     a        All; make this change and all remaining ones without
  201.             further confirmation.
  202.     q        Quit; don't make any more changes.
  203.     l        Last; make this change and then quit.
  204.     CTRL-E        Scroll the text one line up.
  205.     CTRL-Y        Scroll the text one line down.
  206.  
  207.  
  208. The "from" part of the substitute command is actually a pattern.  The same
  209. kind as used for the search command.  For example, this command only
  210. substitutes "the" when it appears at the start of a line: >
  211.  
  212.     :s/^the/these/
  213.  
  214. If you are substituting with a "from" or "to" part that includes a slash, you
  215. need to put a backslash before it.  A simpler way is to use another character
  216. instead of the slash.  A plus, for example: >
  217.  
  218.     :s+one/two+one or two+
  219.  
  220. ==============================================================================
  221. *10.3*    Command ranges
  222.  
  223. The ":substitute" command, and many other : commands, can be applied to a
  224. selection of lines.  This is called a range.
  225.    The simple form of a range is {number},{number}.  For example: >
  226.  
  227.     :1,5s/this/that/g
  228.  
  229. Executes the substitute command on the lines 1 to 5.  Line 5 is included.
  230. The range is always placed before the command.
  231.  
  232. A single number can be used to address one specific line: >
  233.  
  234.     :54s/President/Fool/
  235.  
  236. Some commands work on the whole file when you do not specify a range.  To make
  237. them work on the current line the "." address is used.  The ":write" command
  238. works like that.  Without a range, it writes the whole file.  To make it write
  239. only the current line into a file: >
  240.  
  241.     :.write otherfile
  242.  
  243. The first line always has number one.  How about the last line?  The "$"
  244. character is used for this.  For example, to substitute in the lines from the
  245. cursor to the end: >
  246.  
  247.     :.,$s/yes/no/
  248.  
  249. The "%" range that we used before, is actually a short way to say "1,$", from
  250. the first to the last line.
  251.  
  252.  
  253. USING A PATTERN IN A RANGE
  254.  
  255. Suppose you are editing a chapter in a book, and want to replace all
  256. occurences of "grey" with "gray".  But only in this chapter, not in the next
  257. one.  You know that only chapter boundaries have the word "Chapter" in the
  258. first column.  This command will work then: >
  259.  
  260.     :?^Chapter?,/^Chapter/s=grey=gray=g
  261.  
  262. You can see a search pattern is used twice.  The first "?^Chapter?" finds the
  263. line above the current position that matches this pattern.  Thus the ?pattern?
  264. range is used to search backwards.  Similarly, "/^Chapter/" is used to search
  265. forward for the start of the next chapter.
  266.    To avoid confusion with the slashes, the "=" character was used in the
  267. substitute command here.  A slash or another character would have worked as
  268. well.
  269.  
  270.  
  271. ADD AND SUBTRACT
  272.  
  273. There is a slight error in the above command: If the title of the next chapter
  274. had included "grey" it would be replaced as well.  Maybe that's what you
  275. wanted, but what if you didn't?  Then you can specify an offset.
  276.    To search for a pattern and then use the line above it: >
  277.  
  278.     /Chapter/-1
  279.  
  280. You can use any number instead of the 1.  To address the second line below the
  281. match: >
  282.  
  283.     /Chapter/+2
  284.  
  285. The offsets can also be used with the other items in a range.  Look at this
  286. one: >
  287.  
  288.     :.+3,$-5
  289.  
  290. This specifies the range that starts three lines below the cursor and ends
  291. five lines before the last line in the file.
  292.  
  293.  
  294. USING MARKS
  295.  
  296. Instead of figuring out the line numbers of certain positions, remembering them
  297. and typing them in a range, you can use marks.
  298.    Place the marks as mentioned in chapter 3.  For example, use "mt" to mark
  299. the top of an area and "mb" to mark the bottom.  Then you can use this range
  300. to specify the lines between the marks (including the lines with the marks): >
  301.  
  302.     :'t,'b
  303.  
  304.  
  305. VISUAL MODE AND RANGES
  306.  
  307. You can select text with Visual mode.  If you then press ":" to start a colon
  308. command, you will see this: >
  309.  
  310.     :'<,'>
  311.  
  312. Now you can type the command and it will be applied to the range of lines that
  313. was visually selected.
  314.  
  315.     Note:
  316.     When using Visual mode to select part of a line, or using CTRL-V to
  317.     select a block of text, the colon commands will still apply to whole
  318.     lines.  This might change in a future version of Vim.
  319.  
  320. The '< and '> are actually marks, placed at the start and end of the Visual
  321. selection.  The marks remain at their position until another Visual selection
  322. is made.  Thus you can use the "'<" command to jump to position where the
  323. Visual area started.  And you can mix the marks with other items: >
  324.  
  325.     :'>,$
  326.  
  327. This addresses the lines from the end of the Visual area to the end of the
  328. file.
  329.  
  330.  
  331. A NUMBER OF LINES
  332.  
  333. When you know how many lines you want to change, you can type the number and
  334. then ":".  For example, when you type "5:", you will get: >
  335.  
  336.     :.,.+4
  337.  
  338. Now you can type the command you want to use.  It will use the range "."
  339. (current line) until ".+4" (four lines down).  Thus it spans five lines.
  340.  
  341. ==============================================================================
  342. *10.4*    The global command
  343.  
  344. The ":global" command is one of the more powerful features of Vim.  It allows
  345. you to find a match for a pattern and execute a command there.  The general
  346. form is: >
  347.  
  348.     :[range]global/{pattern}/{command}
  349.  
  350. This is similar to the ":substitute" command.  But, instead of replacing the
  351. matched text with other text, the command {command} is executed.
  352.  
  353.     Note:
  354.     The command executed for ":global" must be one that starts with a
  355.     colon.  Normal mode commands can not be used directly.  The |:normal|
  356.     command can do this for you.
  357.  
  358. Suppose you want to change "foobar" to "barfoo", but only in C++ style
  359. comments.  These comments start with "//".  Use this command: >
  360.  
  361.     :g+//+s/foobar/barfoo/g
  362.  
  363. This starts with ":g".  That is short for ":global", just like ":s" is short
  364. for ":substitute".  Then the pattern, enclosed in plus characters.  Since the
  365. pattern we are looking for contains a slash, this uses the plus character to
  366. separate the pattern.  Next comes the substitute command that changes "foobar"
  367. into "barfoo".
  368.    The default range for the global command is the whole file.  Thus no range
  369. was specified in this example.  This is different from ":substitute", which
  370. works on one line without a range.
  371.    The command isn't perfect, since it also matches lines where "//" appears
  372. halfway a line, and the substitution will also take place before the "//".
  373.  
  374. Just like with ":substitute", any pattern can be used.  When you learn more
  375. complicated patterns later, you can use them here.
  376.  
  377. ==============================================================================
  378. *10.5*    Visual block mode
  379.  
  380. With CTRL-V you can start selection of a rectangular area of text.  There are
  381. a few commands that do something special with the text block.
  382.  
  383. There is something special about using the "$" command in Visual block mode.
  384. When the last motion command used was "$", all lines in the Visual selection
  385. will extend until the end of the line, also when the line with the cursor is
  386. shorter.  This remains effective until you use a motion command that moves the
  387. cursor horizontally.  Thus using "j" keeps it, "h" stops it.
  388.  
  389.  
  390. INSERTING TEXT
  391.  
  392. The command  "I{string}<Esc>" inserts the text {string} in each line, just
  393. left of the visual block.  You start by pressing CTRL-V to enter visual block
  394. mode.  Now you move the cursor to define your block.  Next you type I to enter
  395. Insert mode, followed by the text to insert.  As you type, the text appears on
  396. the first line only.
  397.    After you press <Esc> to end the insert, the text will magically be
  398. inserted in the rest of the lines contained in the visual selection.  Example:
  399.  
  400.     include one ~
  401.     include two ~
  402.     include three ~
  403.     include four ~
  404.  
  405. Move the cursor to the "o" of "one" and press CTRL-V.  Move it down with "3j"
  406. to "four".  You now have a block selection that spans four lines.  Now type: >
  407.  
  408.     Imain.<Esc>
  409.  
  410. The result:
  411.  
  412.     include main.one ~
  413.     include main.two ~
  414.     include main.three ~
  415.     include main.four ~
  416.  
  417. If the block spans short lines that do not extend into the block, the text is
  418. not inserted in that line.  For example, make a Visual block selection that
  419. includes the word "long" in the first and last line of this text, and thus has
  420. no text selected in the second line:
  421.  
  422.     This is a long line ~
  423.     short ~
  424.     Any other long line ~
  425.  
  426.           ^^^^ selected block
  427.  
  428. Now use the command "Ivery <Esc>".  The result is:
  429.  
  430.     This is a very long line ~
  431.     short ~
  432.     Any other very long line ~
  433.  
  434. In the short line no text was inserted.
  435.  
  436. If the string you insert contains a newline, the "I" acts just like a Normal
  437. insert command and affects only the first line of the block.
  438.  
  439. The "A" command works the same way, except that it appends after the right
  440. side of the block.
  441.    There is one special case for "A": Select a Visual block and then use "$"
  442. to make the block extend to the end of each line.  Using "A" now will append
  443. the text to the end of each line.
  444.    Using the same example from above, and then typing "$A XXX<Esc>, you get
  445. this result:
  446.  
  447.     This is a long line XXX ~
  448.     short XXX ~
  449.     Any other long line XXX ~
  450.  
  451. This really requires using the "$" command.  Vim remembers that it was used.
  452. Making the same selection by moving the cursor to the end of the longest line
  453. with other movement commands will not have the same result.
  454.  
  455.  
  456. CHANGING TEXT
  457.  
  458. The Visual block "c" command deletes the block and then throws you into Insert
  459. mode to enable you to type in a string.  The string will be inserted in each
  460. line in the block.
  461.    Starting with the same selection of the "long" words as above, then typing
  462. "c_LONG_<Esc>", you get this:
  463.  
  464.     This is a _LONG_ line ~
  465.     short ~
  466.     Any other _LONG_ line ~
  467.  
  468. Just like with "I" the short line is not changed.  Also, you can't enter a
  469. newline in the new text.
  470.  
  471. The "C" command deletes text from the left edge of the block to the end of
  472. line.  It then puts you in Insert mode so that you can type in a string,
  473. which is added to the end of each line.
  474.    Starting with the same text again, and typing "Cnew text<Esc>" you get:
  475.  
  476.     This is a new text ~
  477.     short ~
  478.     Any other new text ~
  479.  
  480. Notice that, even though only the "long" word was selected, the text after it
  481. is deleted as well.  Thus only the location of the left edge of the visual
  482. block really matters.
  483.    Again, short lines that do not reach into the block are excluded.
  484.  
  485. Other commands that change the characters in the block:
  486.  
  487.     ~    swap case    (a -> A and A -> a)
  488.     U    make uppercase  (a -> A and A -> A)
  489.     u    make lowercase  (a -> a and A -> a)
  490.  
  491.  
  492. FILLING WITH A CHARACTER
  493.  
  494. To fill the whole block with one character, use the "r" command.  Again,
  495. starting with the same example text from above, and then typing "rx":
  496.  
  497.     This is a xxxx line ~
  498.     short ~
  499.     Any other xxxx line ~
  500.  
  501.  
  502.     Note:
  503.     If you want to include characters beyond the end of the line in the
  504.     block, check out the 'virtualedit' feature in chapter 25.
  505.  
  506.  
  507. SHIFTING
  508.  
  509. The command ">" shifts the selected text to the right one shift amount,
  510. inserting whitespace.  The starting point for this shift is the left edge of
  511. the visual block.
  512.    With the same example again, ">" gives this result:
  513.  
  514.     This is a      long line ~
  515.     short ~
  516.     Any other      long line ~
  517.  
  518. The shift amount is specified with the 'shiftwidth' option.  To change it to
  519. use 4 spaces: >
  520.  
  521.     :set shiftwidth=4
  522.  
  523. The "<" command removes one shift amount of whitespace at the left
  524. edge of the block.  This command is limited by the amount of text that is
  525. there; so if there is less than a shift amount of whitespace available, it
  526. removes what it can.
  527.  
  528.  
  529. JOINING LINES
  530.  
  531. The "J" command joins all selected lines together into one line.  Thus it
  532. removes the line breaks.  Actually, the line break, leading white space and
  533. trailing white space is replaced by one space.  Two spaces are used after a
  534. line ending (that can be changed with the 'joinspaces' option).
  535.    Let's use the example that we got so familiar with now.  The result of
  536. using the "J" command:
  537.  
  538.     This is a long line short Any other long line ~
  539.  
  540. The "J" command doesn't require a blockwise selection.  It works with "v" and
  541. "V" selection in exactly the same way.
  542.  
  543. If you don't want the white space to be changed, use the "gJ" command.
  544.  
  545. ==============================================================================
  546. *10.6*    Reading and writing part of a file
  547.  
  548. When you are writing an e-mail message, you may want to include another file.
  549. This can be done with the ":read {filename}" command.  The text of the file is
  550. put below the cursor line.
  551.    Starting with this text:
  552.  
  553.     Hi John, ~
  554.     Here is the diff that fixes the bug: ~
  555.     Bye, Pierre. ~
  556.  
  557. Move the cursor to the second line and type: >
  558.  
  559.     :read patch
  560.  
  561. The file named "patch" will be inserted, with this result:
  562.  
  563.     Hi John, ~
  564.     Here is the diff that fixes the bug: ~
  565.     2c2 ~
  566.     <    for (i = 0; i <= length; ++i) ~
  567.     --- ~
  568.     >    for (i = 0; i < length; ++i) ~
  569.     Bye, Pierre. ~
  570.  
  571. The ":read" command accepts a range.  The file will be put below the last line
  572. number of this range.  Thus ":$r patch" appends the file "patch" at the end of
  573. the file.
  574.    What if you want to read the file above the first line?  This can be done
  575. with the line number zero.  This line doesn't really exist, you will get an
  576. error message when using it with most commands.  But this command is allowed:
  577. >
  578.     :0read patch
  579.  
  580. The file "patch" will be put above the first line of the file.
  581.  
  582.  
  583. WRITING A RANGE OF LINES
  584.  
  585. To write a range of lines to a file, the ":write" command can be used.
  586. Without a range it writes the whole file.  With a range only the specified
  587. lines are written: >
  588.  
  589.     :.,$write tempo
  590.  
  591. This writes the lines from the cursor until the end of the file into the file
  592. "tempo".  If this file already exists you will get an error message.  Vim
  593. protects you from accidentally overwriting an existing file.  If you know what
  594. you are doing and want to overwrite the file, append !: >
  595.  
  596.     :.,$write! tempo
  597.  
  598. CAREFUL: The ! must follow the ":write" command immediately, without white
  599. space.  Otherwise it becomes a filter command, which is explained later in
  600. this chapter.
  601.  
  602.  
  603. APPENDING TO A FILE
  604.  
  605. In the first section of this chapter was explained how to collect a number of
  606. lines into a register.  The same can be done to collect lines in a file.
  607. Write the first line with this command: >
  608.  
  609.     :.write collection
  610.  
  611. Now move the cursor to the second line you want to collect, and type this: >
  612.  
  613.     :.write >>collection
  614.  
  615. The ">>" tells Vim the "collection" file is not to be written as a new file,
  616. but the line must be appended at the end.   You can repeat this as many times
  617. as you like.
  618.  
  619. ==============================================================================
  620. *10.7*    Formatting text
  621.  
  622. When you are typing plain text, it's nice if the length of each line is
  623. automatically trimmed to fit in the window.  To make this happen while
  624. inserting text, set the 'textwidth' option: >
  625.  
  626.     :set textwidth=72
  627.  
  628. You might remember that in the example vimrc file this command was used for
  629. every text file.  Thus if you are using that vimrc file, you were already
  630. using it.  To check the current value of 'textwidth': >
  631.  
  632.     :set textwidth
  633.  
  634. Now lines will be broken to take only up to 72 characters.  But when you
  635. insert text halfway a line, or when you delete a few words, the lines will get
  636. too long or too short.  Vim doesn't automatically reformat the text.
  637.    To tell Vim to format the current paragraph: >
  638.  
  639.     gqap
  640.  
  641. This starts with the "gq" command, which is an operator.  Following is "ap",
  642. the text object that stands for "a paragraph".  A paragraph is separated from
  643. the next paragraph by an empty line.
  644.  
  645.     Note:
  646.     A blank line, which contains white space, does NOT separate
  647.     paragraphs.  This is hard to notice!
  648.  
  649. Instead of "ap" you could use any motion or text object.  If your paragraphs
  650. are properly separated, you can use this command to format the whole file: >
  651.  
  652.     gggqG
  653.  
  654. "gg" takes you to the first line, "gq" is the format operator and "G" the
  655. motion that jumps to the last line.
  656.  
  657. In case your paragraphs aren't clearly defined, you can format just the lines
  658. you manually select.  Move the cursor to the first line you want to format.
  659. Start with the command "gqj".  This formats the current line and the one below
  660. it.  If the first line was short, words from the next line will be appended.
  661. If it was too long, words will be moved to the next line.  The cursor moves to
  662. the second line.  Now you can use "." to repeat the command.  Keep doing this
  663. until you are at the end of the text you want to format.
  664.  
  665. ==============================================================================
  666. *10.8*    Changing case
  667.  
  668. You have text with section headers in lowercase.  You want to make the word
  669. "section" all uppercase.  Do this with the "gU" operator.  Start with the
  670. cursor in the first column: >
  671.  
  672.                  gUw
  673. <    section header        ---->      SECTION header
  674.  
  675. The "gu" operator does exactly the opposite: >
  676.  
  677.                  guw
  678. <    SECTION header        ---->      section header
  679.  
  680. You can also use "g~" to swap case.  All these are operators, thus they work
  681. with any motion command, with text objects and in Visual mode.
  682.    To make an operator work on lines you double it.  The delete operator is
  683. "d", thus to delete a line you use "dd".  Similarly, "gugu" makes a whole line
  684. lowercase.  This can be shortened to "guu".  "gUgU" is shortened to "gUU" and
  685. "g~g~" to "g~~".  Example: >
  686.  
  687.                 g~~
  688. <    Some GIRLS have Fun    ---->   sOME girls HAVE fUN ~
  689.  
  690. ==============================================================================
  691. *10.9*    Using an external program
  692.  
  693. Vim has a very powerful set of commands, it can do anything.  But there may
  694. still be something that an external command can do better or faster.
  695.    The command "!{motion}{program}" takes a block of text and filters it
  696. through an external program.  In other words, it runs the system command
  697. represented by {program}, giving it the block of text represented by {motion}
  698. as input.  The output of this command then replaces the selected block.
  699.    Because this summarizes badly if you are unfamiliar with UNIX filters, take
  700. a look at an example.  The sort command sorts a file.  If you execute the
  701. following command, the unsorted file input.txt will be sorted and written to
  702. output.txt. (This works on both UNIX and Microsoft Windows.) >
  703.  
  704.     sort <input.txt >output.txt
  705.  
  706. Now do the same thing in Vim.  You want to sort lines 1 through 5 of a file.
  707. You start by putting the cursor on line 1.  Next you execute the following
  708. command: >
  709.  
  710.     !5G
  711.  
  712. The "!" tells Vim that you are performing a filter operation.  The Vim editor
  713. expects a motion command to follow, indicating which part of the file to
  714. filter.  The "5G" command tells Vim to go to line 5, so it now knows that it
  715. is to filter lines 1 (the current line) through 5.
  716.    In anticipation of the filtering, the cursor drops to the bottom of the
  717. screen and a ! prompt displays.  You can now type in the name of the filter
  718. program, in this case "sort".  Therefore, your full command is as follows: >
  719.  
  720.     !5Gsort<Enter>
  721.  
  722. The result is that the sort program is run on the first 5 lines.  The output
  723. of the program replaces these lines.
  724.  
  725.     line 55                  line 11
  726.     line 33                  line 22
  727.     line 11        -->          line 33
  728.     line 22                  line 44
  729.     line 44                  line 55
  730.     last line              last line
  731.  
  732. The "!!" command filters the current line through a filter.  In Unix the "date"
  733. command prints the current time and date.  "!!date<Enter>" replaces the current
  734. line with the output of "date".  This is useful to add a timestamp to a file.
  735.  
  736.  
  737. WHEN IT DOESN'T WORK
  738.  
  739. Starting a shell, sending it text and capturing the output requires that Vim
  740. knows how the shell works exactly.  When you have problems with filtering,
  741. check the values of these options:
  742.  
  743.     'shell'        specifies the program that Vim uses to execute
  744.             external programs.
  745.     'shellcmdflag'    argument to pass a command to the shell
  746.     'shellquote'    quote to be used around the command
  747.     'shellxquote'    quote to be used around the command and redirection
  748.     'shelltype'    kind of shell (only for the Amiga)
  749.     'shellslash'    use forward slashes in the command (only for
  750.             MS-Windows and alikes)
  751.     'shellredir'    string used to write the command output into a file
  752.  
  753. On Unix this is hardly ever a problem, because there are two kinds of shells:
  754. "sh" like and "csh" like.  Vim checks the 'shell' option and sets related
  755. options automatically, depending on whether it sees "csh" somewhere in
  756. 'shell'.
  757.    On MS-Windows, however, there are many different shells and you might have
  758. to tune the options to make filtering work.  Check the help for the options
  759. for more information.
  760.  
  761.  
  762. READING COMMAND OUTPUT
  763.  
  764. To read the contents of the current directory into the file, use this:
  765.  
  766. on Unix: >
  767.     :read !ls
  768. on MS-Windows: >
  769.     :read !dir
  770.  
  771. The output of the "ls" or "dir" command is captured and inserted in the text,
  772. below the cursor.  This is similar to reading a file, except that the "!" is
  773. used to tell Vim that a command follows.
  774.    The command may have arguments.  And a range can be used to tell where Vim
  775. should put the lines: >
  776.  
  777.     :0read !date -u
  778.  
  779. This inserts the current time and date in UTC format at the top of the file.
  780. (Well, if you have a date command that accepts the "-u" argument.)  Note the
  781. difference with using "!!date": that replaced a line, while ":read !date" will
  782. insert a line.
  783.  
  784.  
  785. WRITING TEXT TO A COMMAND
  786.  
  787. The Unix command "wc" counts words.  To count the words in the current file: >
  788.  
  789.     :write !wc
  790.  
  791. This is the same write command as before, but instead of a file name the "!"
  792. character is used and the name of an external command.  The written text will
  793. be passed to the specified command as its standard input.  The output could
  794. look like this:
  795.  
  796.        4      47     249 ~
  797.  
  798. The "wc" command isn't verbose.  This means you have 4 lines, 47 words and 249
  799. characters.
  800.  
  801. Watch out for this mistake: >
  802.  
  803.     :write! wc
  804.  
  805. This will write the file "wc" in the current directory, with force.  White
  806. space is important here!
  807.  
  808.  
  809. REDRAWING THE SCREEN
  810.  
  811. If the external command produced an error message, the display may have been
  812. messed up.  Vim is very efficient and only redraws those parts of the screen
  813. that it knows need redrawing.  But it can't know about what another program
  814. has written.  To tell Vim to redraw the screen: >
  815.  
  816.     CTRL-L
  817.  
  818. ==============================================================================
  819.  
  820. Next chapter: |usr_11.txt|  Recovering from a crash
  821.  
  822. Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl:
  823.